(:  
   Merge (ordered by the 1st date in each item)
   2 or more "display" files
 :)

declare boundary-space strip;
import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
    
declare variable $items := doc($arguments[1])/display/item ;

declare function local:get-self-children($it as node()) as node()*
{
      <item>{
      <info name="id">{xs:string($it/info[@name="src-anchor"]/anchor)}</info>,
      <info name="date">{fn:substring(jj:editDate(jj:unix-to-dateTime(xs:integer($it/info[@name="time"]/time/date))),1,10)}</info>,
      for $info in $it/info
      return 
        if ($info/@name="message") then
          <info name="message">{jj:remove-html($info[@name="message"]/text())}</info>
        else 
          $info
      }</item>,
      for $item in $items where $item/info[@name="parentid"] = xs:string($it/info[@name="src-anchor"]/anchor)
      return
      	local:get-self-children($item)
};

<display>{
  let $posts := $items[info[@name="type"]!="comment"]
  for $post at $pos in $posts
  order by $post/info[@name="time"]/time/date[1] 
  return
  local:get-self-children($post)
}</display>
